/*
 * SunSpotGloveRemoteTest1App.java
 *
 * Created on 2009-11-20 18:44:05;
 */
package org.sunspotworld;

import com.sun.spot.io.j2me.radiogram.*;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.IAccelerometer3D;
import com.sun.spot.util.*;

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * The startApp method of this class is called by the VM to start the
 * application.
 *
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 */
public class SunSpotGloveRemoteTest1App extends MIDlet {

    private static final int HOST_PORT = 100;
    private static final int SAMPLE_FREQUENCY = 10;  // in HZ
    private RadiogramConnection conn;
    private Datagram dg;
    private IAccelerometer3D accel;
    private static final boolean CONNECT_ONCE = false;

    public SunSpotGloveRemoteTest1App() {
        this.accel = EDemoBoard.getInstance().getAccelerometer();
    }

    // Start midlet
    protected void startApp() throws MIDletStateChangeException {
        new BootloaderListener().start();   // monitor the USB (if connected) and recognize commands from host

        String ourAddress = System.getProperty("IEEE_ADDRESS");
        System.out.println("Remote radio address = " + ourAddress);

        startAcceWatchThreadd();
    }

    public void startAcceWatchThreadd() {

        new Thread() {

            public void run() {
                try {
                    if (CONNECT_ONCE) {
                        conn = (RadiogramConnection) Connector.open("radiogram://broadcast:" + HOST_PORT);
                        dg = conn.newDatagram(conn.getMaximumLength());
                    }

                    long time = System.currentTimeMillis();

                    while (true) {

                        time = System.currentTimeMillis();

                        String msg = checkingMovements();
                        sendOut(msg);

                        try {
                            long sleep = Math.max(0, (1000 / SAMPLE_FREQUENCY - (System.currentTimeMillis() - time)));
                            Thread.sleep(sleep);
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }.start();
    }

    private String checkingMovements() {

        String msg = "";
        try {
            msg = accel.getRelativeAccelX() + ";" + accel.getRelativeAccelY() + ";" + accel.getRelativeAccelZ();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return msg;
    }

    private synchronized void sendOut(String msg) {

        try {
            if (!CONNECT_ONCE) {
                conn = (RadiogramConnection) Connector.open("radiogram://broadcast:" + HOST_PORT);
                dg = conn.newDatagram(conn.getMaximumLength());
            }

            try {
                dg.writeUTF(msg);
                conn.send(dg);
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (!CONNECT_ONCE) {
                conn.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void pauseApp() {
        // This will never be called by the Squawk VM
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // Only called if startApp throws any exception other than MIDletStateChangeException
    }
}
